import {setSessionJson, getSessionJson, setSessionValue, getSessionValue, removeSession} from "../../utils/auth";
import loginApi from "../../api/login";

let userKey = "mm-login-user-web";
let tokenKey = "mm-user-token-web";
const user = {
    state: {
        user: getSessionJson(userKey),
        token: getSessionValue()
    },
    mutations: {
        SET_USER(state, user) {
            state.user = user;
            setSessionJson(userKey, user);
        },
        SET_TOKEN(state, jwtToken) {
            state.token = jwtToken;
            setSessionValue(tokenKey, jwtToken);
        }
    },
    actions: {
        /**
         * 手机号/邮箱/用户名 + 密码 + 图形验证码 登录
         */
        PwdLogin({commit}, data) {
            return new Promise((resolve, reject) => {
                loginApi
                    .pwdLogin(data.loginData,data.type)
                    .then(resp => {
                        const respData = resp.data;
                        if (respData.flag) {
                            commit("SET_TOKEN", respData.data);
                        }
                        resolve(respData);
                    })
                    .catch(error => {
                        reject(error);
                    });
            });
        },
        /**
         *手机+短信 登录
         */
        MobileLogin({commit}, data) {
            return new Promise((resolve, reject) => {
                loginApi
                    .mobileLogin(data.loginData,data.type)
                    .then(resp => {
                        const respData = resp.data;
                        if (respData.flag) {
                            commit("SET_TOKEN", respData.data);
                        }
                        resolve(respData);
                    })
                    .catch(error => {
                        reject(error);
                    });
            });
        },

        /**
         * 登录后获取用户信息
         */
        GetUserInfo({commit}) {
            return new Promise((resolve, reject) => {
                loginApi
                    .userInfo()
                    .then(resp => {
                        const respData = resp.data;
                        commit("SET_USER", respData.data);
                        resolve(respData);
                    })
                    .catch(error => {
                        reject(error);
                    });
            });
        },
        //退出
        Logout({commit}) {
            return new Promise((resolve, reject) => {
                loginApi
                    .logout()
                    // eslint-disable-next-line no-unused-vars
                    .then(resp => {
                        console.log(resp)
                        let respData = {
                            flag: true
                        };
                        if (resp.data.flag){
                            commit("SET_USER", "");
                            commit("SET_TOKEN", "");
                            removeSession(userKey)
                            removeSession(tokenKey)
                        }else{
                            respData.flag=false;
                        }
                        resolve(respData);

                    });
            });
        }
    }
};
export default user;
